class Node:  
    """默认构造函数"""  
    def __init__(self, value = None, next_node=None):  
        self.value = value    # 节点的值  
        self.next = next_node # 指向下一个节点的引用 
    

class LinkedList:  
    """定义链表"""  
    def __init__(self):  
        self.head = None # 链表的头节点 

    # 头插  
    def push_front(self, value):  
        """在链表头部插入一个新节点"""  
        new_node = Node(value)  
        new_node.next = self.head # 新节点的 next 指向当前头节点  
        self.head = new_node      # 更新头节点为新节点  
  
    # 头删  
    def pop_front(self):  
        """删除链表头部的节点并返回其值"""  
        if self.head is None:  
            raise IndexError("Cannot pop from an empty linked list.")  
        res = self.head.value      # 获取要删除节点的值  
        self.head = self.head.next # 更新头节点为下一个节点  
        return res  
  
    # 尾插  
    def push_back(self, value):  
        """在链表尾部插入一个新节点"""  
        new_node = Node(value)  
        if self.head is None:    # 如果链表为空，新节点成为头节点  
            self.head = new_node  
        else:  
            curr = self.head     # 遍历到链表尾部
            while curr.next is not None:    
                curr = curr.next  
            curr.next = new_node # 在尾部插入新节点  
  
    # 尾删  
    def pop_back(self):  
        """删除链表尾部的节点并返回其值"""  
        if self.head is None:  
            raise IndexError("Cannot pop from an empty linked list.")  
        if self.head.next is None: # 如果链表只有一个节点  
            res = self.head.value  
            self.head = None       # 清空头节点  
            return res
          
        curr = self.head           # 遍历到倒数第二个节点
        while curr.next.next is not None:    
            curr = curr.next  
        res = curr.next.value      # 获取要删除节点的值  
        curr.next = None           # 删除尾节点  
        return res  
    
    # 查找
    def search(self, target):  
            """查找链表中第一个值为target的节点，并返回其节点对象"""  
            curr = self.head  
            while curr is not None:  
                if curr.value == target:  
                    return curr # 返回找到的节点对象  
                curr = curr.next  
            return None         # 如果未找到，返回None
  
    # 修改  
    def update(self, old_value, new_value):    
        """修改链表中第一个值为old_value的节点的值为new_value，并返回是否成功修改"""    
        node = self.search(old_value) # 查找节点    
        if node is not None:    
            node.value = new_value    
            return True               # 修改成功    
        return False                  # 未找到要修改的节点 
  
    # 打印  
    def print_list(self):  
        """打印链表中的所有节点值"""  
        curr = self.head  
        while curr is not None:  
            print(f"{curr.value}->", end="")  
            curr = curr.next  
        print("None")  

    
    # 倒数第k个节点的值
    def find_kth_from_end(self, k):  
        """找出单向链表中倒数第 k 个节点的值"""  
        # 初始化两个指针，都指向头节点  
        fast = self.head  
        slow = self.head  
            
        # 先让快指针向前移动 k 步  
        for _ in range(k):  
            if fast is None:  
                # 如果 k 大于链表长度，返回 None 或抛出异常（这里选择返回 None）  
                return None  
            fast = fast.next  
            
        # 然后两个指针同时移动，直到快指针到达链表末尾  
        while fast is not None:  
            fast = fast.next  
            slow = slow.next  
            
        # 此时慢指针指向的节点就是倒数第 k 个节点  
        return slow.value if slow is not None else None  
  
# 示例用法  
"""if __name__ == "__main__":  
    ll = LinkedList()   
    for value in [1, 2, 3, 4, 5]:  
        ll.push_back(value)  
      
    k = [1, 2, 3, 4, 5, 6]
    for i in k:
        result = ll.find_kth_from_end(i)  
        print(f"倒数第 {i} 个节点的值是: {result}")"""


import random

ls = [random.randint(1, 6) for _ in range(11)]
ls_reversed = list(reversed(ls))

def print_list(ls):
    for i in ls:
        print(i, end = "")
    print()

print_list(ls)
print_list(ls_reversed)

